#encoding = utf-8

#玩家模块
import pygame
#import monster
from BasicsModules import BasicsMonster
from function import MathAngleAndDirection
import math

class Player(pygame.sprite.Sprite):

    def __init__(self, bgsize, win, strImageType = "typePig", strName = "闪电猪"):
        pygame.sprite.Sprite.__init__(self)
        #基础属性
        #判断选择的形象,并确定攻击类型和各项能力倍数
        if strImageType == "typePig":
            self.name = "pig"
            self.image = pygame.image.load("images/heropig01.png").convert_alpha()
            self.attackType = "combat"  #攻击类型，combat近战，longrange远程
        elif strImageType == "typeMonkey":
            self.name = "monkey"
            self.image = ""
            self.attackType = "longrange"
        elif strImageType == "typeRabbit":
            self.name = "rabbit"
            self.image = ""
            self.attackType = "combat"
        elif strImageType == "typeDragon":
            self.name = "dragon"
            self.image = ""
            self.attackType = "longrange"
        self.win = win  #记录窗体
        self.playerName = strName
        self.fontSize = 12  #名字字体大小
        self.baseAttack = 50   #基础攻击力
        self.baseDefense = 5   #基础防御力
        self.baseSpeed = 2    #基础速度
        self.baseDouble = 1 #速度倍数
        self.blood = 1000  #血量
        self.fullBlood = self.blood #记录满血血量
        self.bloodHeight = 10   #血条高度
        self.direction = ["", ""] #方向集合   (上下),(左右)
        self.attackDirection = []   #攻击方向
        self.attackDirDelayTime = 10     #方向判断延迟
        self.attackRange = 10   #攻击范围
        self.attackInterval = self.attackTimer = 60   #self.attackInterval攻击间隔时间,self.attackTimer攻击计时
        self.rect = self.image.get_rect()   #图像的尺寸信息
        self.width, self.height = bgsize[0], bgsize[1]  #窗体高度
        self.rect.left, self.rect.top = (self.width - self.rect.width)/2, (self.height - self.rect.height)/2

    def checkRelativePosition(self, direction):
        bolUp = True
        bolDown = True
        bolRight = True
        bolLeft = True
        for m in BasicsMonster.BasMonster.lstMonster:
            top = m.rect.top - self.rect.top
            left = m.rect.left - self.rect.left
            spacing = int(math.sqrt(top * top + left * left))
            maad = MathAngleAndDirection().mathAngleToDirection(left, top)
            if direction == "up":
                if spacing <= m.rect.height and maad == 0:
                    bolUp = False

            if direction == "down":
                if spacing <= m.rect.height and maad == 4:
                    bolDown = False

            if direction == "right":
                if spacing <= m.rect.width and maad == 2:
                    bolRight = False

            if direction == "left":
                if spacing <= m.rect.width and maad == 6:
                    bolLeft = False

            if not bolLeft:
                break
            if not bolUp:
                break
            if not bolDown:
                break
            if not bolRight:
                break

        if len(BasicsMonster.BasMonster.lstMonster) > 0:
            if bolUp and direction == "up":
                self.rect.top -= self.baseSpeed
            if bolDown and direction == "down":
                self.rect.top += self.baseSpeed
            if bolRight and direction == "right":
                self.rect.left += self.baseSpeed
            if bolLeft and direction == "left":
                self.rect.left -= self.baseSpeed
        else:
            if direction == "up":
                self.rect.top -= self.baseSpeed
            if direction == "down":
                self.rect.top += self.baseSpeed
            if direction == "left":
                self.rect.left -= self.baseSpeed
            if direction == "right":
                self.rect.left += self.baseSpeed


    def moveUp(self):
        self.direction[0] = "up"
        if self.rect.top > self.rect.height + self.bloodHeight:
            self.checkRelativePosition(self.direction[0])
        else:
            self.rect.top = self.rect.height + self.bloodHeight

    def moveDown(self):
        self.direction[0] = "down"
        if self.rect.bottom < self.height:
            self.checkRelativePosition(self.direction[0])
        else:
            self.rect.bottom = self.height

    def moveLeft(self):
        self.direction[1] = "left"
        if self.rect.left > 0:
            self.checkRelativePosition(self.direction[1])
        else:
            self.rect.left = 0

    def moveRight(self):
        self.direction[1] = "right"
        if self.rect.right < self.width:
            self.checkRelativePosition(self.direction[1])
        else:
            self.rect.right = self.width

    def setDirectionNull(self):
        self.direction = ["", ""]

    def realAttack(self, realDirection):
        for m in BasicsMonster.BasMonster.lstMonster:
            top = m.rect.top
            left = m.rect.left
            x = left - self.rect.left
            y = top - self.rect.top
            z = math.sqrt(x * x + y * y)
            myRange = math.sqrt(self.rect.height * self.rect.height + self.rect.width * self.rect.width)
            maad = MathAngleAndDirection()
            if realDirection == ["up", ""]:
                if maad.mathAngleToDirection(x, y) == 0:
                    if z <= self.rect.height + self.attackRange:
                        m.reduceBlood(self.baseAttack)
                        break
            elif realDirection == ["up", "right"]:
                if maad.mathAngleToDirection(x, y) == 1:
                    if z <= myRange + self.attackRange:
                        m.reduceBlood(self.baseAttack)
                        break
            elif realDirection == ["", "right"]:
                if maad.mathAngleToDirection(x, y) == 2:
                    if z <= self.rect.width + self.attackRange:
                        m.reduceBlood(self.baseAttack)
                        break
            elif realDirection == ["down", "right"]:
                if maad.mathAngleToDirection(x, y) == 3:
                    if z <= myRange + self.attackRange:
                        m.reduceBlood(self.baseAttack)
                        break
            elif realDirection == ["down", ""]:
                if maad.mathAngleToDirection(x, y) == 4:
                    if z <= self.rect.height + self.attackRange:
                        m.reduceBlood(self.baseAttack)
                        break
            elif realDirection == ["down", "left"]:
                if maad.mathAngleToDirection(x, y) == 5:
                    if z <= myRange + self.attackRange:
                        m.reduceBlood(self.baseAttack)
                        break
            elif realDirection == ["", "left"]:
                if maad.mathAngleToDirection(x, y) == 6:
                    if z <= self.rect.height + self.attackRange:
                        m.reduceBlood(self.baseAttack)
                        break
            elif realDirection == ["up", "left"]:
                if maad.mathAngleToDirection(x, y) == 7:
                    if z <= myRange + self.attackRange:
                        m.reduceBlood(self.baseAttack)
                        break

    def attack(self):
        if self.attackTimer == self.attackInterval:
            self.attackTimer -= 1
            self.realAttack(self.attackDirection)


    def reduceBlood(self, attack):
        if attack - self.baseDefense < 0:
            self.blood -= 1
        else:
            self.blood -= attack - self.baseDefense

        if self.blood <= 0:
            pass

    def drawName(self):
        top = self.rect.top - self.bloodHeight - self.fontSize - 3
        widthDifference = int((len(self.playerName) * self.fontSize - self.rect.width) / 2)
        left = self.rect.left - widthDifference
        textName = pygame.font.Font("fonts/msyh.ttf", self.fontSize)
        nameImage = textName.render(self.playerName, True, (255, 255, 255))
        self.win.blit(nameImage, (left, top))

    def bloodStrip(self):
        top = self.rect.top - self.bloodHeight
        left = self.rect.left
        width = self.rect.width
        blood = int(self.blood / self.fullBlood * (width - 1))

        bloodFrame = (0, 0, 0)  #黑色
        bloodColor = (51, 102, 255)    #蓝色
        bloodBack = (51, 51, 51)    #深灰色

        pygame.draw.line(self.win, bloodFrame, [left + 1, top], [left + width - 1, top], 1)
        pygame.draw.line(self.win, bloodFrame, [left + 1, top + self.bloodHeight - 2], [left + width - 1, top + self.bloodHeight - 2], 1)
        pygame.draw.line(self.win, bloodFrame, [left, top + 1], [left, top + self.bloodHeight - 3], 1)
        pygame.draw.line(self.win, bloodFrame, [left + width, top + 1], [left + width, top + self.bloodHeight - 3], 1)
        pygame.draw.line(self.win, bloodBack, [left + 1, top + int((self.bloodHeight - 3)/2 + 0.5)], [left + width - 1, top + int((self.bloodHeight - 3)/2 + 0.5)], self.bloodHeight - 3)
        pygame.draw.line(self.win, bloodColor, [left + 1, top + int((self.bloodHeight - 3)/2 + 0.5)], [left + blood, top + int((self.bloodHeight - 3)/2 + 0.5)], self.bloodHeight - 3)

    def draw(self):
        if self.attackTimer <= 0:
            self.attackTimer = 60
        elif self.attackTimer > 0 and self.attackTimer < self.attackInterval:
            self.attackTimer -= 1
        self.win.blit(self.image, self.rect)
        self.bloodStrip()
        self.drawName()



#测试的代码
if __name__ == "__main__":
    from pygame.locals import *
    import sys
    testwin = pygame.display.set_mode((300, 300))
    me = Player(testwin, 100, 100)
    #bgImage = pygame.image.load('images/background.png').convert()  #背景图片

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print("exit")
                exit()
            #elif event.type == pygame.KEYDOWN:
        keyPress = pygame.key.get_pressed()
        if keyPress[K_w] or keyPress[K_UP]:
            me.moveUp()
        if keyPress[K_s] or keyPress[K_DOWN]:
            me.moveDown()
        if keyPress[K_a] or keyPress[K_LEFT]:
            me.moveLeft()
        if keyPress[K_d] or keyPress[K_RIGHT]:
            me.moveRight()
        testwin.fill([3, 3, 3])
        me.draw()
        pygame.time.delay(6)
        pygame.display.update() #加载更新
